const {DataTypes} = require("sequelize");

const seq = require("../db/seq");
const Type = require("./type.model");
const Record = require("./record.model");
const User = require("./user.model");
// 创建模型(Model zd_user -> 表 zd_users)
const Category = seq.define(
    "category",
    {
        // id 会被sequelize自动创建, 管理
        title: {
            type: DataTypes.STRING,
            allowNull: false,
            comment: "项目标题",
        },
        imgUrl: {
            type: DataTypes.STRING,
            allowNull: false,
            comment: "项目图片",
        },
        describe: {
            type: DataTypes.TEXT,
            allowNull: true,
            comment: "项目描述",
        },
        expected_amount: {
            type: DataTypes.INTEGER,
            allowNull: true,
            comment: "期望金额",
        },
        end_time: {
            type: DataTypes.DATE,
            allowNull: true,
            comment: "众筹结束时间",
        },
        amount_raised: {
            type: DataTypes.DECIMAL(10, 2),
            allowNull: true,
            comment: "已筹集金额",
            defaultValue: 0,
        },
        record_id: {
            type: DataTypes.INTEGER,
            allowNull: false,
            comment: "记录id,存储点赞、收藏、浏览数据",
        },
        user_id: {
            type: DataTypes.INTEGER,
            allowNull: false,
            comment: "用户id,创建项目人",
        },
        type_id: {
            type: DataTypes.INTEGER,
            allowNull: false,
            comment: "项目类型id",
        },
        status: {
            type: DataTypes.INTEGER,
            allowNull: true,
            comment: "项目状态：0 待审核 1 审核通过 2 审核不通过 3 已过期",
        },
    },
    {
        paranoid: true,
    }
);
Category.belongsTo(Record, {
    foreignKey: "record_id",
    as: "record_info",
});
Category.belongsTo(User, {
    foreignKey: "user_id",
    as: "user_info",
});
Category.belongsTo(Type, {
    foreignKey: "type_id",
    as: "type_info",
});
// 强制同步数据库(创建数据表)
// Category.sync({ force: true });

module.exports = Category;
